<?php


namespace app\crontab\controller;
use app\crontab\model\Commodity;
use think\facade\Request;


/***  定时任务
 * Class Crontab
 * @package app\crontab\controller
 */
class CrontabCommodity
{

    //获取商品
    public function obtainCommodity(){
        $lastNum = Request::param('lastNum');
        //防重复
        $file_name ="/usr/local/nginx/html/order_assistant/public/uploads/CrontabCommodity/obtainCommodity.txt";
        $fz = file_exists($file_name);
        $buffer = 0;
        $str = time();
        $time = $str;
        if($fz){
            $fp=fopen($file_name,'r');
            while(!feof($fp))
            {
                $buffer=fgets($fp,4096);
            }

            fclose($fp);
        }
        $buffer=$buffer + 5;
        if($buffer  > $time){
//            echo '试用活动开奖定时任务--重复执行终止';
            //Log::record('.抢购订单20分钟未购买做订单超时处理订单关闭');
            exit;
        }

        $file_pointer = fopen($file_name,"w");
        fwrite($file_pointer,$str);
        fclose($file_pointer);
        
        $pub_id=307;//身份ID 
        $key = 'lZr5W264cQkB6FLLLD7fOSRfOD12f1KM';//身份ID 
        
        $url = 'http://api.act.jutuike.com/dyfx/product_search';//抖音精选联盟商品搜索API接口
        $detailed = [];
        for($i=$lastNum;$i<$lastNum+5;$i++){
            $time = time();
            $data = [
                'pub_id' => $pub_id,
                'page' => $i,
                'page_size' => 20,
                'price_min' =>1,
                'price_max' =>90,
            ];
            $http_post_json = self::http_post($url,$data);
            
            $return = json_decode($http_post_json,true);
            
            if($return['total_results'] == 2000 && $return['msg'] == "success" && $return['code'] == 1){
                $where = [];
                $where[] = ['enable','=','1'];
                $where[] = ['addtime','>',strtotime(date('Y-m-d',$time))];
                $where[] = ['addtime','<',strtotime(date('Y-m-d',$time+24*3600*2))];
                $comall = Commodity::commodityListAll($where);
                $comArray = array();
                if(!empty($comall)){
                    $comall = $comall->toArray();
                    
                    foreach ($comall as $v){
                        array_push($comArray,$v['product_id']);
                    }
                }
                
                if(!empty($return['data'])){
                    foreach ($return['data'] as $value) {
                        if(empty($value['product_id'])){
                            continue;
                        }
                        
                        if(in_array($value['product_id'],$comArray)){
                            continue;
                        }
                        else{
                            array_push($comArray,$value['product_id']);
                        }
                        $detailed[] = [
                            'product_id' => $value['product_id'],
                            'title' => $value['title'],
                            'detail_url' => $value['detail_url'],
                            'shop_id' => $value['shop_id'],
                            'shop_name' => $value['shop_name'],
                            'price' => $value['price']/100,
                            'cos_ratio' => $value['cos_ratio'],
                            'cos_fee' => $value['cos_fee']/100,
                            'first_cid' => $value['first_cid'],
                            'second_cid' => $value['second_cid'],
                            'third_cid' => $value['third_cid'],
                            'addtime' => $time,
                        ];
                    }
                }
            }
        }

        if(!empty($detailed)){
            Commodity::commodityAddAll($detailed); 
        }
        
    }
    
    
    public function transmissionCommodity(){
        $url = 'http://49.234.42.121:8010/api/CommodityController/commodityList';//抖音精选联盟商品搜索API接口
        $time = time();
        $data = [
            'page' => 1,
            'num' => 100,
        ];
        $http_post_json = self::http_post($url,$data);
        $return = json_decode($http_post_json,true);
        if(!empty($return['data'])){
            foreach ($return['data'] as $value) {
                $detailed[] = [
                    'product_id' => $value['product_id'],
                    'title' => $value['title'],
                    'detail_url' => $value['detail_url'],
                    'shop_id' => $value['shop_id'],
                    'shop_name' => $value['shop_name'],
                    'price' => $value['price'],
                    'cos_ratio' => $value['cos_ratio'],
                    'cos_fee' => $value['cos_fee'],
                    'first_cid' => $value['first_cid'],
                    'second_cid' => $value['second_cid'],
                    'third_cid' => $value['third_cid'],
                    'addtime' => $time,
                ];
            }
        }
        
        if(!empty($detailed)){
            Commodity::commodityAddAll($detailed); 
        }
    }
    
    
    public static function http_post($url,$data=array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);

        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
    }



}